package com.kfm.hotel.controller;

import com.github.pagehelper.PageInfo;
import com.kfm.hotel.domain.RoomType;
import com.kfm.hotel.domain.SysUser;
import com.kfm.hotel.service.RoomTypeService;
import com.kfm.hotel.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

@RestController
@RequestMapping("/roomType")
public class RoomTypeController {

    @Autowired
    private RoomTypeService service;

    @RequestMapping
    public ModelAndView index() {
        ModelAndView modelAndView = new ModelAndView("roomType/index");
        return modelAndView;
    }


    @GetMapping("/list")
    public R list(@RequestParam(defaultValue = "1") Integer page,
                  @RequestParam(defaultValue = "10") Integer limit,
                  RoomType roomType) {
        PageInfo<RoomType> pageInfo = service.list(roomType, page, limit);
        return R.ok(pageInfo);
    }

    @GetMapping("/add")
    public ModelAndView add() {
        ModelAndView modelAndView = new ModelAndView("roomType/add");
        return modelAndView;
    }

    @PostMapping("/add")
    public R add(@RequestBody RoomType roomType) throws Exception {

        boolean flag = service.save(roomType);

        if (flag) {
            return R.ok("添加成功", roomType.getId());
        } else {
            return R.fail("添加失败");
        }
    }
}
